package com.uns1066.unsbar.action;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.uns1066.unsbar.common.CommonConstant;
import com.uns1066.unsbar.util.FileUploadUtil;
import com.uns1066.unsbar.util.ImgListPath;
import com.uns1066.unsbar.util.SpringUtil;

@Controller
@RequestMapping({ "/file" })
public class FileAction {
	public static final String STATUS = "status";
	public static final String MSG = "msg";

	@RequestMapping({ "/index" })
	public String index(Model model) {
		return "file/index";
	}

	@ResponseBody
	@RequestMapping({ "/upload" })
	public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file, Long fileType, Long userId) {
		Map jsonMap = new HashMap();
		try {
			Long maxUploadSize = (long) 10485760 * 10;
			if (file.getSize() > maxUploadSize) {
				jsonMap.put("STATUS", Integer.valueOf(-1));
				jsonMap.put("MSG", "不能上传超过100M的文件");
				return jsonMap;
			}
			Assert.isTrue(userId != null, "userId is not null");

			jsonMap.putAll(FileUploadUtil.uploadFile(file, fileType, userId));
			jsonMap.put("status", Integer.valueOf(0));
		} catch (Exception e) {
			jsonMap.put("status", Integer.valueOf(-1));
			jsonMap.put("MSG", e.getMessage());
		}
		return jsonMap;
	}

	@ResponseBody
	@RequestMapping({ "/uploads" })
	public Map<String, Object> uploadFiles(@RequestParam MultipartFile[] files, Long fileType, Long userId) {
		Map jsonMap = new HashMap();
		return jsonMap;
	}

	@ResponseBody
	@RequestMapping({ "/getFilesCount" })
	public String getFilesCount() throws Exception {
		File f = new File(CommonConstant.getLocalPath());
		if (ImgListPath.lis.size() == 0) {
			printDirectory(f, ImgListPath.lis);
		}
		return ImgListPath.lis.size() + "";
	}

	@ResponseBody
	@RequestMapping({ "/dispose" })
	public String dispose() {
		for (int i = 1; i <= 16; i++) {
			new Thread() {
				public void run() {
					while (true) {
						String path = ImgListPath.getPath();
						if (path == null)
							break;
						File f = new File(path);
						int index = f.getPath().lastIndexOf(".");
						String fileType = f.getPath().substring(index);
						String fileName = f.getPath().substring(0, index);
						try {
							FileUploadUtil.createZoom(f, f.getPath(), fileType, fileName, Long.valueOf(0L));
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}

			}

			.start();
		}
		return ImgListPath.lis.size() + "";
	}

	@ResponseBody
	@RequestMapping({ "/show" })
	public String show() {
		return ImgListPath.lis.size() + "";
	}

	public void printDirectory(File f, ArrayList<String> lis) throws Exception {
		if (!f.isDirectory()) {
			if (f.getName().indexOf("_") <= 0) {
				String[] type = CommonConstant.getFileType();
				int index = f.getPath().lastIndexOf(".");
				String fileType = f.getPath().substring(index);
				boolean uploadFileTye = SpringUtil.containsElement(type, fileType);
				if (uploadFileTye)
					lis.add(f.getPath());
			}
		} else {
			File[] fs = f.listFiles();
			for (int i = 0; i < fs.length; i++) {
				File file = fs[i];
				printDirectory(file, lis);
			}
		}
	}
}